package com.yao.shop.service.impl;

import com.yao.shop.common.Page;
import com.yao.shop.enums.StateEnums;
import com.yao.shop.mapper.SysRoleMapper;
import com.yao.shop.mapper.SysUserMapper;
import com.yao.shop.mapper.SysUserRoleMapper;
import com.yao.shop.pojo.SysRole;
import com.yao.shop.pojo.SysUser;
import com.yao.shop.pojo.SysUserRole;
import com.yao.shop.pojo.vo.SysUserVo;
import com.yao.shop.service.SysUserService;
import com.yao.shop.utils.IdWorker;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public void updateLoginTime(String username) {
        sysUserMapper.updateLoginTime(username);
    }

    @Override
    public SysUser getByUsername(String username) {
        return sysUserMapper.getByUsername(username);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysUserVo sysUser){
        SysUser user = new SysUser();
        BeanUtils.copyProperties(sysUser, user);
        //id用雪花算法生成
        long userId = idWorker.nextId();
        user.setId(userId);
        sysUserMapper.save(user);
        // 如果角色id存在，存入用户角色表
        if (sysUser.getSysRole() != null && sysUser.getSysRole().getRoleId() != null) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleId(sysUser.getSysRole().getRoleId());
            sysUserRoleMapper.save(sysUserRole);
        }
    }

    @Override
    public void update(SysUserVo sysUser){
        // 拷贝属性
        SysUser user = new SysUser();
        BeanUtils.copyProperties(sysUser, user);
        sysUserMapper.update(user);
        // 不管前端有没有选择角色，我们都把旧的角色信息删掉，如果选择了，就再添加新的角色信息
        sysUserRoleMapper.deleteByUserId(user.getId());
        // 如果角色id存在，存入用户角色表
        if (sysUser.getSysRole() != null && sysUser.getSysRole().getRoleId() != null) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(user.getId());
            sysUserRole.setRoleId(sysUser.getSysRole().getRoleId());
            sysUserRoleMapper.save(sysUserRole);
        }
    }

    @Override
    public void delete(Long id){
        sysUserMapper.delete(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void enable(Long id){
        //先查后改
        SysUser sysUser=sysUserMapper.getById(id);
        sysUser.setStatus(StateEnums.ENABLED.getCode());
        sysUserMapper.updateStatus(sysUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disable(Long id){
        // 先查再改
        SysUser sysUser = sysUserMapper.getById(id);
        sysUser.setStatus(StateEnums.NOT_ENABLE.getCode());
        sysUserMapper.updateStatus(sysUser);
    }

    @Override
    public Page<SysUser> getByPage(Page<SysUser> page) {
        // 设置默认的当前页和每页条数
        Integer pageNumber = page.getPageNumber();
        if (pageNumber == null || pageNumber < 1) {
            pageNumber = 1;
            page.setPageNumber(pageNumber);
        }
        List<SysUser> userList = sysUserMapper.getByPage(page);
        Integer totalCount = sysUserMapper.countByPage(page);
        page.setList(userList);
        page.setTotalCount(totalCount);
        return page;
    }

    @Override
    public SysUserVo get(Long id) {
        SysUser user = sysUserMapper.getById(id);
        // 拷贝信息
        SysUserVo sysUserVo = new SysUserVo();
        BeanUtils.copyProperties(user, sysUserVo);
        // 查询角色信息
        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.getByUserId(user.getId());
        if (!CollectionUtils.isEmpty(sysUserRoleList)) {
            // 说明有角色信息，取出角色ID
            List<Long> roleIds = sysUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
            // 根据角色ID去查询所有的角色信息
            List<SysRole> roleList = sysRoleMapper.getByIds(roleIds);
            if (!CollectionUtils.isEmpty(roleList)) {
                sysUserVo.setSysRole(roleList.get(0));
            }
        }
        return sysUserVo;
    }
}
